/*
 * @Title: 姿美汇H5商城 - 大礼包列表
 * @Author: JSL
 * @Date:   2018-11-02
 * @Last Modified by:  JSL
 * @Last Modified time: 2018-11-02 16:15:41
 */
var dataInfo = new Object(),
  template = {
    init: function() {
      Vue.use(VueLazyload, {
        preLoad: 1.3,
        error: 'https://h5.meetao.com/img/product/init.png',
        loading: 'https://h5.meetao.com/img/product/init.png',
        attempt: 1
      });
      dataInfo.init = new Vue({
        el: '#app',
        data: {
          listData: [],
          tabbarData: ['自购省钱', '分享赚钱', '免费课程', '专属服务'],
          tabData: ['超值开店礼包', 'VIP权益'],
          vipRules: ['http://pic.zimeitang.cn/group1/M00/0C/30/cjf1-FvlOGqAXJ9GAAIOxBKiQHE613.jpg', 'http://pic.zimeitang.cn/group1/M00/19/AF/cjf1-FvlOLaAMhhkAARcG7Nritw918.jpg'],// 'http://pic.zimeitang.cn/group1/M00/14/1B/ecToXVvlRMuAZEKXAAKgY3TWjYU994.jpg',
          isApp: !getQueryString('isApp') ? 0 : 1,
          isGiftVip: !getQueryString('isGiftVip') ? getQueryString('logInStatus') == 0 ? 2 : 0 : getQueryString('isGiftVip'), //有手机号！=0 为vip 无手机号 1为已登录为店主 2.为未登录等同于vip 即不等于0 只有0是店主
          icon: {
            normal: '../img/index/shouhui.png',
            active: '../img/index/shouhong.png',
            normal1: '../img/index/gouhui.png',
            active1: '../img/index/gouhong.png',
            active2: '../img/index/myhong.png',
            normal2: '../img/index/myhui.png',
            active3: '../img/index/viphong.png',
            normal3: '../img/index/viphui.png',
          },
          tabbarStatus: 1,
          hasStore: 0, //是否是店主 初始化-0 第一次是-1 已经是-2
          active: 0,
          loading: false,
          isLoading: false,
          finished: false,
          cartShow: false, //规格选择
          specificationData: { //购物车规格
            inventory: "",
            specificationList: [],
            secondActive: 0, //默认选中规格
            specificationDetail: {}, //规格详情
            num: 1, //数量
            goodsCode: ""
          },
          appInfoEnter: false,
          userName: '',
          userPhone: '',
        },
        methods: {
          backApp: function() {
            location.href = "jg://com.simeitol.www?type=backAppMine";
          },
          toNext: function(data) { //列表购买
            console.log(data);
            if (this.isApp == 1) { //立即购买
              if (this.isGiftVip == 2) { //未登录
                location.href = "jg://com.simeitol.www?type=toAppLogIn";
              } else if (!getQueryString('isGiftVip')) { //邀请好友
                location.href = "jg://com.simeitol.www?type=inviteVipStorer";
              } else { //app内部购买
                dataInfo.init.getAppGiftPayInfo(data.goodsItems[0].goodsCode);
              };
            } else { //app外部直接购买
              this.specificationData.specificationList = data.goodsItems;
              this.specificationData.inventory = data.totalInventory;
              this.specificationData.secondActive = 0;
              this.specificationData.specificationDetail = {
                imgUrl: data.imgUrl.indexOf('group1/M00') < 0 ? data.imgUrl : data.imgUrl + '_200x8000.jpg',
                inventory: data.totalInventory,
                price: data.price,
                specifications: this.specificationData.specificationList[0].specifications
              };
              this.specificationData.goodsCode = this.specificationData.specificationList[0].goodsCode;
              // 判断大礼包规格数量 ==1 直接购买 >1用户自己选择
              if (!!userId) { //验证是否是登录用户
                // this.getUserPhone();
                this.queryJudge();
              } else { //未登录跳转登录
                jumpLogin();
              };
              // dataInfo.init.specificationData.specificationList.length > 1 ? dataInfo.init.cartShow = !dataInfo.init.cartShow : dataInfo.init.submitGiftInfo();
              // data.goodsItems.length > 1 ? this.cartShow = !this.cartShow : this.submitGiftInfo();
            };
          },
          queryJudge: function() { //181207 新增 验证用户身份
            ajaxTemplate(addressCodeUrl + SERVICE.queryJudge, function(data) {
              console.log(data);
              if (data.tag == 0 && data.data.result.bindingCode == 0) { // bindingCode ==0 分销用户 、bindingCode ==1 直营用户 跳转登录
                dataInfo.init.getUserPhone();
              } else {
                jumpLogin();
              };
            }, {
              userId: userId
            });
          },
          getUserPhone: function() { //回显用户手机号
            if (!!dataInfo.init.userPhone) { //手机号已存在 直接验证用户身份
              this.getUserIdentity();
            } else {
              ajaxTemplate(addressUrl + SERVICE.getGiftUserPhone, function(data) {
                console.log(data);
                if (data.tag == 0) {
                  dataInfo.init.userPhone = data.data.result.phone;
                  dataInfo.init.getUserIdentity();
                  // dataInfo.init.appInfoEnter = true; //引导用户填写手机号
                } else {
                  dataInfo.init.$toast(data.errMsg);
                };
              }, {
                userId: userId
              });
            };
          },
          getUserIdentity: function() {
            ajaxTemplate(addressCodeUrl + SERVICE.fxDzPhone, function(data) {
              console.log(data);
              if (data.tag == 0) {
                data.data.result.userRole > 2 ? dataInfo.init.$toast('您已是VIP店主') : dataInfo.init.appInfoEnter = true; //引导用户填写手机号
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              phone: dataInfo.init.userPhone,
            });
          },
          focusMouse: function() { //姓名录入
            dataInfo.baginStartTime = setInterval(function() { //设置一个计时器，时间设置与软键盘弹出所需时间相近
              document.body.scrollTop = document.body.scrollHeight; //获取焦点后将浏览器内所有内容高度赋给浏览器滚动部分高度
            }, 200);
          },
          blurMouse: function() { //失去光标事件
            clearInterval(dataInfo.baginStartTime); //清除计时器
            document.body.scrollTop = dataInfo.baginStartTime;
          },
          getAppGiftPayInfo: function(e) { //录入用户真实姓名 app内部只有vip才能看到大礼包页 所以不必单独判断
            dataInfo.appGiftPayInfo = e;
            this.appInfoEnter = true;
          },
          toAppGiftPay: function() { //app端大礼包 - vip仅需填写姓名
            if (!!this.userName) {
              ajaxTemplate(addressUrl + SERVICE.fxNameSave, function(data) {
                console.log(data);
                if (data.tag == 0) {
                  dataInfo.init.appInfoEnter = false;
                  if (dataInfo.init.isApp == 0 || !isApp) { //H5购买
                    dataInfo.init.specificationData.specificationList.length > 1 ? dataInfo.init.cartShow = !dataInfo.init.cartShow : dataInfo.init.submitGiftInfo();
                  } else { //app购买
                    location.href = "jg://com.simeitol.www?type=toFxDLBOrders&productId=" + dataInfo.appGiftPayInfo;
                  };
                } else {
                  dataInfo.init.$toast(data.errMsg);
                };
              }, {
                userId: isApp == 1 ? getQueryString('uid_') : userId,
                realName: this.userName
              }, {
                nologin: 1
              });
            };
          },
          specificationDetail: function(data) {
            this.specificationData.secondActive = data;
            this.specificationData.specificationDetail.specifications = this.specificationData.specificationList[this.specificationData.secondActive].specifications;
            this.specificationData.goodsCode = this.specificationData.specificationList[data].goodsCode;
          },
          toProduct: function(data) { //跳转商品详情
            if (!!isApp) {
              if (this.isGiftVip == 2) { //未登录
                location.href = "jg://com.simeitol.www?type=toAppLogIn";
              } else {
                dataInfo.toAppGiftProductDetails = window.location.href.split('/indexOfGift.html')[0] + "/productDetails.html?isFx=1&productId=" + data + '&uid_=' + getQueryString('uid_') + '&m_=' + getQueryString('m_') + '&v_=' + getQueryString('v_') + '&pt_=' + getQueryString('pt_') + '&access_token=' + getQueryString('access_token');
                console.log(dataInfo.toAppGiftProductDetails);
                location.href = "jg://com.simeitol.www?type=toAppGiftProductDetails&productUrl=" + dataInfo.toAppGiftProductDetails;
              };
            } else {
              jumpProductDetails(data + '&uid_=' + getQueryString('uid_') + '&m_=' + getQueryString('m_') + '&v_=' + getQueryString('v_') + '&pt_=' + getQueryString('pt_') + '&access_token=' + getQueryString('access_token'));
            };
          },
          submitGiftInfo: function() { //直接购买大礼包
            !userId ? jumpLogin() : '';
            console.log('生成订单');
            dataInfo.submitInfo = this.specificationData.specificationList[this.specificationData.secondActive];
            dataInfo.init.$toast.loading({
              forbidClick: true,
              message: '数据提交中...',
              loadingType: 'spinner',
              duration: 0,
            });
            ajaxTemplate(addressUrl + SERVICE.orderforh5, function(data) { //填写订单查询 - 无优惠券
              dataInfo.init.$toast.clear();
              if (data.tag == 0) {
                //data.data.result 增加邮费 改变跳转带参
                window.location.href = "orderFilling.html?isGiftPacksOrder=1&orderData=" + escape(JSON.stringify({
                  goodsCode: dataInfo.submitInfo.goodsCode,
                  num: 1,
                  isKill: 1
                }));
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              userId: userId,
              goodsInfo: JSON.stringify([{
                goodsCode: dataInfo.submitInfo.goodsCode,
                num: 1,
                isKill: 1
              }]),
              isGiftPacksOrder: 0
            });
          },
          getlistData: function() { //  dataInfo.init.getlistData();
            !this.isApp ? document.title = '姿美汇VIP店主' : document.title = '邀请VIP店主';
            ajaxTemplate(addressUrl + SERVICE.queryGiftPacks, function(data) {
              console.log(data);
              var daTa = data.data.result;
              if (data.tag == 0) {
                dataInfo.init.listData = daTa;
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              userId: userId
            }, {
              nologin: 1
            });
          },
          downLoad: function() {
            this.hasStore == 1 ? downAppFun() : this.hasStore = 0;
          },
        },
        watch: {
          active: function(e) {
            console.log(e);
            // this.page = 1;
            // this.showNum = 1;
            // this.listData = [];
            // document.body.scrollTop = 0;
            // document.documentElement.scrollTop = 0;
            // dataInfo.init.getList();
          }
        },
      });
      //init
      dataInfo.init.getlistData();
    },
    iosInfoHide: function() { //ios 弹窗隐藏 template.iosInfoHide();
      dataInfo.init.appInfoEnter = false;
    },
  };
template.init();
